package com.controller.sys;


import com.common.Result;
import com.common.log.OperationLog;
import com.common.log.OperationType;
import com.entity.sys.Suggest;
import com.entity.sys.User;
import com.service.sys.SuggestService;
import com.service.sys.UserService;
import com.vo.sys.SuggestVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author clay
 * @since 2022-03-08
 */
@RestController
@RequestMapping("/suggest")
public class SuggestController {
    @Autowired
    private SuggestService suggestService;

    @Autowired
    private UserService userService;

    @ApiOperation("通过id获取建议")
    @GetMapping("info/{id}")
    public Result info(@PathVariable String id) {
        Suggest suggest = suggestService.getById(id);
        return Result.success(copyUser(suggest));
    }

    @ApiOperation("添加建议")
    @PostMapping("/add")
    @OperationLog(operationType = OperationType.INSERT, describe = "添加建议信息", entity = Suggest.class, primaryKey = "id")
    public Result add(@RequestBody Suggest suggest) {
        String id = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        suggest.setCreateBy(id);
        suggest.setUpdateBy(id);
        if (suggestService.save(suggest)) {
            return Result.success("操作成功");
        }
        return Result.fail("添加失败!");
    }

    @ApiOperation("修改建议")
    @OperationLog(operationType = OperationType.INSERT, describe = "修改建议信息", entity = Suggest.class, primaryKey = "id")
    @PostMapping("/update")
    public Result update(@RequestBody Suggest suggest) {
        String id = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        suggest.setUpdateBy(id);
        if (suggestService.updateById(suggest)) {
            return Result.success("操作成功");
        }
        return Result.fail("修改失败!");
    }

    @ApiOperation("删除建议")
    @OperationLog(operationType = OperationType.INSERT, describe = "删除建议信息", entity = Suggest.class, primaryKey = "id")
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        if (suggestService.removeById(id)) {
            return Result.success("操作成功");
        }
        return Result.fail("删除失败!");
    }

    @ApiOperation("获取所有建议")
    @GetMapping("/all")
    public Result all() {

        return Result.success(copyUserList(suggestService.list()));
    }

    @ApiOperation("查找建议")
    @PostMapping("/select")
    public Result select(@RequestBody Suggest suggest) {
        return Result.success(copyUserList(suggestService.select(suggest)));
    }

    private List<SuggestVo> copyUserList(List<Suggest> suggestList) {
        List<SuggestVo> suggestVos = new ArrayList<>();
        suggestList.forEach(suggest -> {
            suggestVos.add(copyUser(suggest));
        });
        return suggestVos;
    }

    private SuggestVo copyUser(Suggest suggest) {
        SuggestVo vo = new SuggestVo();
        BeanUtils.copyProperties(suggest, vo);
        vo.setUserInfo(userService.getById(suggest.getUserId()));
        vo.setReplyUserInfo(userService.getById(suggest.getReplyUserId()));
        return vo;
    }
}
